package za.org.jcicada.genss.lifecycle.impl;

import static org.junit.Assert.assertNotNull;
import za.org.jcicada.genss.SystemURN;
import za.org.jcicada.genss.lifecycle.ILifeCycleClient;
import za.org.jcicada.genss.lifecycle.LogLevel;
import za.org.jcicada.genss.messages.Result;

public class StubLifeCycleClient implements ILifeCycleClient {

    public boolean disconnected;
    public Result result;
    public SystemURN systemURN;
    public LogLevel level;
    public boolean pinged;

    int TIMEOUT = 1000;

    public void clear() {
    	disconnected = false;
        result = null;
        level = null;
    }
    
    public void informDisconnect() {
        disconnected = true;
    }

    public void replyHalt(Result result, SystemURN systemURN) {
        this.result = result;
        this.systemURN = systemURN;
    }

    public void replyRestart(Result result, SystemURN systemURN) {
        this.result = result;
        this.systemURN = systemURN;
    }

    public void replySetLogLevel(Result result, SystemURN systemURN, LogLevel level) {
        this.result = result;
        this.systemURN = systemURN;
        this.level = level;
    }
    
    public void replyReconnect(Result result, SystemURN systemURN) {
        this.result = result;
        this.systemURN = systemURN;
    }
    
    public void replyPing(Result result, SystemURN systemURN) {
    	this.result = result;
    	this.systemURN = systemURN;
    	this.pinged = true;
    }

    public Result getResult() {
        int remaining = TIMEOUT;
        while (remaining > 0) {
            if (result != null) {
                break;
            }
            remaining = remaining - 10;
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            } 
        }
        assertNotNull("Expected a result.", result);
        return result;
    }

}
